<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2016-2017 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//
namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 公告
 *
 * @author  Xmh
 */
class ClcwAnnounce
{
    private $announceModel;

    public function __construct()
    {
        $this->announceModel = M("announcement", 'clcw_', 'DB_CLCW');
    }

    /**
     * 获取公告列表
     *
     * @param type $page
     * @param type $size
     * @param type $params
     *
     * @return array
     */
    public function getList($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 20 : (int)$size;
        $map = [];
        if (!empty($params['an_title'])) {
            $map['an_title'] = ['like', "%" . $params['an_title'] . "%"];
        }
        $cols = "id,emp_id,status,an_title,an_content,create_time,publish_time";
        $total = $this->announceModel->field($cols)
            ->where($map)
            ->count();
        $list = $this->announceModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order('id DESC')
            ->select();

        $empId = [];
        foreach ($list as $v) {
            $empId[] = $v['emp_id'];
        }

        array_map('strip_tags', $list);
        $result = [
            'list'   => $list,
            'total'  => $total,
            'emp_id' => join(',', $empId)
        ];
        return $result;
    }

    /**
     * 获取公告详情
     *
     * @param type $annId
     *
     * @return type
     */
    public function getInfo($annId)
    {
        if (empty($annId)) {
            AuctionException::make_throw('100056');
        }
        $cols = 'an_title,an_content,create_time';
        $res = $this->announceModel->field($cols)->find($annId);
        return $res;
    }

    /**
     * 获取公告详情
     *
     * @param int $id
     *
     * @return array
     */
    public function getAnnounce($id)
    {
        if (empty($id)) {
            return false;
        }
        return $this->announceModel->where(['id' => $id])->find();
    }

    /**
     * 添加公告数据
     *
     * @param array $data
     */
    public function addAnnounce($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['create_time'] = NOW_TIME;
        return $this->announceModel->add($data);
    }

    /**
     * 编辑公告
     */
    public function saveAnnounce($id, $data = [])
    {
        if (empty($id) || empty($data)) {
            return false;
        }
        $data['update_time'] = NOW_TIME;
        return $this->announceModel->where(['id' => $id])->save($data);
    }
}
